//
//  SJRefreshControl.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/8.
//  Copyright © 2015年 world. All rights reserved.
//  自定义刷新控件

import UIKit

class SJRefreshControl: UIRefreshControl {

    
    
    //view
    lazy var refreshView: SJRefreshView = SJRefreshView.refreshView()
    override init() {
        super.init()
        addSubview(refreshView)
        
        //布局refreshView
        refreshView.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: CGSize(width: 150, height: 60))
        
        //使用KVO监听
        addObserver(self, forKeyPath: "frame", options: NSKeyValueObservingOptions(rawValue: 0), context: nil)
        
    }
    /**
    监听到frame的改变
    */
    //是否显示箭头标识
    var showArrowFlag = false
    //是否在刷新标识
    var isRefreshingFlag = false
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
//        print(frame)
        if frame.origin.y > 0 {
            return
        }
        ///当刷新控件正在刷新的时候
        if refreshing && !isRefreshingFlag{
            refreshView.startRefreshAnimating()
            return
        }
        
        if frame.origin.y < -50 && showArrowFlag == false{
            showArrowFlag = true
            refreshView.rotateLoadingArrow(showArrowFlag)
//            print("翻过来")
        }else if frame.origin.y > -50 && showArrowFlag == true{
            showArrowFlag = false
            refreshView.rotateLoadingArrow(showArrowFlag)

//            print("转过去")
        }
    }
    
    /**
    重新refreshControl的endRefresh方法,在该方法中调用endRefreshAnimating方法
    */
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.endRefreshAnimationg()
    }
    
    /**
    移除KVO的监听
    */
    deinit{
        removeObserver(self, forKeyPath: "frame")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class SJRefreshView: UIView {
    
    @IBOutlet weak var loadingView: UIView!
    @IBOutlet weak var loadingArrow: UIImageView!
    @IBOutlet weak var loadingIcon: UIImageView!
    
   class func refreshView()->SJRefreshView{
        //返回的是anyObject? 需要使用as! 类型转换
     return NSBundle.mainBundle().loadNibNamed("SJRefreshView", owner: nil, options: nil).last as! SJRefreshView
    }
  
    /**
    开始刷新动画
    */
    private func startRefreshAnimating(){
        loadingView.hidden = true
        let anima = CABasicAnimation()
        anima.toValue = 2 * M_PI
        anima.keyPath = "transform.rotation"
        anima.repeatCount = MAXFLOAT
        anima.duration = 0.5
        loadingIcon.layer.addAnimation(anima, forKey: nil)
    }
    /**
    结束刷新动画
    */
    private func endRefreshAnimationg(){
        loadingIcon.layer.removeAllAnimations()
        loadingView.hidden = false
    }
    
    /**
    旋转箭头
    
    - parameter closeWrise: 是否顺时针
    */
    private func rotateLoadingArrow(closeWrise:Bool){
        
        
        UIView.animateWithDuration(0.25) { () -> Void in
            var angle = M_PI
            angle += closeWrise ? -0.01 : +0.01

            self.loadingArrow.transform = CGAffineTransformRotate(self.loadingArrow.transform, CGFloat(angle))
        }
    }
}